#include <lvgl.h>
#include <TFT_eSPI.h>
#include <Arduino.h>

#include "led.h"
#include "beep.h"

#include "lvgl_login.h"

/*Change to your screen resolution*/
static const uint16_t screenWidth  = 320;
static const uint16_t screenHeight = 240;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * screenHeight / 10 ];

TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */


/* Display flushing */
void my_disp_flush( lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p )
{
    uint32_t w = ( area->x2 - area->x1 + 1 );
    uint32_t h = ( area->y2 - area->y1 + 1 );

    tft.startWrite();
    tft.setAddrWindow( area->x1, area->y1, w, h );
    tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
    tft.endWrite();

    lv_disp_flush_ready( disp_drv );
}

/*Read the touchpad*/
void my_touchpad_read( lv_indev_drv_t * indev_drv, lv_indev_data_t * data )
{
    uint16_t touchX, touchY;

    bool touched = tft.getTouch( &touchX, &touchY, 600 );

    if( !touched )
    {
        data->state = LV_INDEV_STATE_REL;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;

        /*Set the coordinates*/
        data->point.x = touchX * 1.33;
        data->point.y = touchY * 0.75;

        Serial.print( "Data x " );
        Serial.println( data->point.x );

        Serial.print( "Data y " );
        Serial.println( data->point.y );
    }
}
// LED点击回调函数
static void btn_event_handler(lv_event_t * obj) {
    lv_event_code_t code = lv_event_get_code(obj);
	switch (code)
	{
	case LV_EVENT_CLICKED:
	{
        ledState = !ledState;
        digitalWrite(LED1_PIN, ledState ? HIGH : LOW);
        digitalWrite(LED2_PIN, ledState ? HIGH : LOW);
        digitalWrite(BEEP_PIN, HIGH);
        delay(100);
        digitalWrite(BEEP_PIN, LOW);
	}
	break;
	default:
		break;
	}
}
void setup()
{
    Serial.begin( 115200 ); // usart初始化
    led_Init();             // LED初始化
    beep_Init();            // beep初始化

    lv_init();              // lvgl初始化

    tft.begin();          /* TFT init */
    tft.setRotation( 3 ); /* Landscape orientation, flipped */

    /*Set the touchscreen calibration data,
     the actual data for your display can be acquired using
     the Generic -> Touch_calibrate example from the TFT_eSPI library*/
    uint16_t calData[5] = { 362, 3530, 282, 3505, 1 };
    tft.setTouch(calData);

    lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * screenHeight / 10 );

    /*Initialize the display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init( &disp_drv );
    /*Change the following line to your display resolution*/
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register( &disp_drv );

    /*Initialize the (dummy) input device driver*/
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init( &indev_drv );
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register( &indev_drv );

/******************************************** lvgl代码编写 ****************************/
    login_Window();
/******************************************** lvgl代码编写 ****************************/
}

void loop()
{
    lv_timer_handler(); /* let the GUI do its work */
    delay( 5 );
}
